﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media;
using System.IO.IsolatedStorage;
using Eventhere.Work;

namespace Eventhere.Account
{
    public partial class Account : PhoneApplicationPage
    {
        User user;

        public Account()
        {
            InitializeComponent();
            this.tgOnOff.Checked += new EventHandler<RoutedEventArgs>(tgOnOff_Checked);
            this.tgOnOff.Unchecked += new EventHandler<RoutedEventArgs>(tgOnOff_Unchecked);
            if (IsolatedStorageSettings.ApplicationSettings.Contains("User"))
                this.user = (User)IsolatedStorageSettings.ApplicationSettings["User"];
            else
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void tgOnOff_Unchecked(object sender, RoutedEventArgs e)
        {
            panelPassword.Visibility = Visibility.Collapsed;
            tgOnOff.Content = "Off";
            tbSurname.IsReadOnly = true;
            tbLastName.IsReadOnly = true;
            tbMail.IsReadOnly = true;
            btConfirm.Visibility = Visibility.Collapsed;
        }

        private void tgOnOff_Checked(object sender, RoutedEventArgs e)
        {
            panelPassword.Visibility = Visibility.Visible;
            tgOnOff.Content = "On";
            btConfirm.Visibility = Visibility.Visible;
            tbSurname.IsReadOnly = false;
            tbLastName.IsReadOnly = false;
            tbMail.IsReadOnly = false;
            tbSurname.Text = user.SurName;
            tbLastName.Text = user.LastName;
            tbMail.Text = user.MailContact;
        }

        private void btConfirm_Click(object sender, RoutedEventArgs e)
        {
            if (tbLastName.Text != user.LastName)
                user.LastName = tbLastName.Text;
            if (tbSurname.Text != user.SurName)
                user.SurName = tbSurname.Text;
            
            this.tgOnOff_Unchecked(this.tgOnOff, new RoutedEventArgs());
            this.tgOnOff.IsChecked = false;
        }

        private void PhoneApplicationPage_Loaded_1(object sender, RoutedEventArgs e)
        {
            tbSurname.Text = user.SurName;
            tbLastName.Text = user.LastName;
            tbMail.Text = user.MailContact;
        }
    }
}